-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = 'morseCode'
VERSION = '1.0.0'

-- 引入必要的库文件(lua编写), 内部库不需要require
local sys = require 'sys'
log.info('main', 'hello world')

print(_VERSION)
width = 160
height = 80

--添加硬狗防止程序卡死
if wdt then
    wdt.init(15000)--初始化watchdog设置为15s
    sys.timerLoopStart(wdt.feed, 10000)--10s喂一次狗
end

spi_lcd = spi.deviceSetup(5,pin.PC14,0,0,8,96*1000*1000,spi.MSB,1,1)
lcd.init("st7735",{port = "device",pin_dc = pin.PE08 ,pin_rst = pin.PC12,pin_pwr = pin.PE09,direction = 1,w = width,h = height,xoffset = 0,yoffset = 24},spi_lcd)
lvgl.init()

function change(text)
    print(text)
    lvgl.textarea_set_text(textarea,text)
end

local input = ''
local text = ''
local scr = lvgl.obj_create(nil, nil)
lvgl.scr_load(scr)

textarea = lvgl.textarea_create(scr, nil);
lvgl.obj_set_size(textarea,150,70)
    -- lvgl.obj_align(textarea, scr, lvgl.ALIGN_CENTER, 0, 0)
lvgl.obj_set_pos(textarea, 5, 5)
lvgl.textarea_set_text(textarea,text)


local pressed = 0
local release = 0

gpio.setup(pin.PE07, function(val)
end, gpio.PULLUP) -- 按键按下接地，因此需要上拉

function getword(morse_code)
    local char = 'unknow'
    if input == '.-' then
        char = "A"
    end
    if input == '-...' then
        char = "B"
    end
    if input == '-.-.' then
        char = "C"
    end
    if input == '-..' then
        char = "D"
    end
    if input == '.' then
        char = "E"
    end
    if input == '..-.' then
        char = "F"
    end
    if input == '--.' then
        char = "G"
    end
    if input == '....' then
        char = "H"
    end
    if input == '..' then
        char = "I"
    end
    if input == '.---' then
        char = "J"
    end
    if input == '-.-' then
        char = "K"
    end
    if input == '.-..' then
        char = "L"
    end
    if input == '--' then
        char = "M"
    end
    if input == '-.' then
        char = "N"
    end
    if input == '---' then
        char = "O"
    end
    if input == '.--.' then
        char = "P"
    end
    if input == '--.-' then
        char = "Q"
    end
    if input == '.-.' then
        char = "R"
    end
    if input == '...' then
        char = "S"
    end
    if input == '-' then
        char = "T"
    end
    if input == '..-' then
        char = "U"
    end
    if input == '...-' then
        char = "V"
    end
    if input == '.--' then
        char = "W"
    end
    if input == '-..-' then
        char = "X"
    end
    if input == '-.--' then
        char = "Y"
    end
    if input == '--..' then
        char = "Z"
    end

    if input == '-----' then
        char = "0"
    end
    if input == '.----' then
        char = "1"
    end
    if input == '..---' then
        char = "2"
    end
    if input == '...--' then
        char = "3"
    end
    if input == '....-' then
        char = "4"
    end
    if input == '.....' then
        char = "5"
    end
    if input == '-....' then
        char = "6"
    end
    if input == '--...' then
        char = "7"
    end
    if input == '---..' then
        char = "8"
    end
    if input == '----.' then
        char = "9"
    end
    return char
end

sys.timerLoopStart(function()
    local val = gpio.get(pin.PE07)
    if val==0 then
        release = 0
        pressed = pressed + 1
        print('pressed',pressed)
    end

    if val==1 and pressed >0  then
        local char = ''
        if pressed < 5 then
            char = "."
        else
            char = "-"
        end
        input = input .. char
        lvgl.textarea_add_text(textarea,char)
        pressed = 0
    end

    if val==1 then
        release = release + 1
    end

    if release > 20 then
        local char = getword(input)

        local len = string.len(input)

        if len > 0 then
            for i=1,len do
                lvgl.textarea_del_char(textarea)
            end
            input = ''
    
            if char ~= 'unknow' then
                lvgl.textarea_add_text(textarea,char)
            end
        end
        -- enter
        release = 0
    end
end, 100)

sys.run()
-- sys.run()之后后面不要加任何语句!!!!!